package com.koicarp.university.graduate.web.controller;

import com.koicarp.university.graduate.auth.annotation.OperLog;
import com.koicarp.university.graduate.auth.constant.ModuleNameEnum;
import com.koicarp.university.graduate.auth.constant.OperateLogTypeEnum;
import com.koicarp.university.graduate.service.entity.SysRole;
import com.koicarp.university.graduate.service.entity.SysRoleMenuRela;
import com.koicarp.university.graduate.service.service.role.ISysRoleService;
import com.koicarp.university.graduate.service.vo.pesonalManage.SysRoleInsVo;
import com.koicarp.university.graduate.service.vo.pesonalManage.SysRoleVo;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 【用户管理：角色表】(SysRole)表控制层
 *
 * @author liutao
 * @since 2020-09-03 19:53:53
 */
@RestController
@RequestMapping("sysRole")
@Slf4j
@RequiredArgsConstructor
@Api(tags = "(角色表)表控制层")
public class SysRoleController {
    /**
     * 服务对象
     */

    private final ISysRoleService sysRoleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SysRole selectOne(Long id) {
        return this.sysRoleService.queryById(id);
    }
    @PostMapping("getAllSysRole")
    @ApiOperation(value = "角色信息-普通查询",notes = "角色信息-普通查询")
    public UniversityResponse getAllSysRole(){
        UniversityResponse response=null;
        try{
            response = sysRoleService.getAllSysRole();
        }catch (Exception e){
            log.error("查询失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }


    @PostMapping("getSysRoleWithPage")
    @ApiOperation(value = "角色信息-分页",notes = "角色信息-普通查询")
    public UniversityResponse getSysRoleWithPage(@RequestBody SysRoleVo vo){
        UniversityResponse response=null;
        try{
            response = sysRoleService.getSysRoleWithPage(vo);
        }catch (Exception e){
            log.error("查询失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
    @PostMapping("saveRoleInfo")
    @ApiOperation(value = "添加新角色",notes = "添加新角色-加权限")
    @OperLog(moduleName = ModuleNameEnum.RoleManage,type = OperateLogTypeEnum.UPDATE,operDesc = "添加新角色-加权限")
    public UniversityResponse saveRoleInfo(@RequestBody SysRoleInsVo vo){
        UniversityResponse response=null;
        try{
            response = sysRoleService.insert(vo);
        }catch (Exception e){
            log.error("添加角色失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
    @PostMapping("removeSysRole")
    @ApiOperation(value = "删除角色",notes = "删除角色-加权限")
    @OperLog(moduleName = ModuleNameEnum.RoleManage,type = OperateLogTypeEnum.DELETE,operDesc = "删除角色-加权限")
    public UniversityResponse removeSysRole(@RequestBody SysRole sysRole){
        UniversityResponse response=null;
        try{
            response = sysRoleService.removeSysRole(sysRole);
        }catch (Exception e){
            log.error("删除角色失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
    @PostMapping("modifySysRole")
    @ApiOperation(value = "修改角色信息",notes = "修改角色信息-加权限")
    @OperLog(moduleName = ModuleNameEnum.RoleManage,type = OperateLogTypeEnum.UPDATE,operDesc = "修改角色信息-加权限")
    public UniversityResponse removeSysRole(@RequestBody SysRoleInsVo vo){
        UniversityResponse response=null;
        try{
            response = sysRoleService.modifySysRole(vo);
        }catch (Exception e){
            log.error("修改角色信息失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    @PostMapping("getMenuIdByRoleId")
    @ApiOperation(value = "根据roleid找相关的menuId",notes = "")
    public UniversityResponse getMenuIdByRoleId(@RequestBody SysRoleMenuRela sysRoleMenuRela){
        UniversityResponse response=null;
        try{
            response = sysRoleService.getMenuIdByRoleId(sysRoleMenuRela);
        }catch (Exception e){
            log.error("修改角色信息失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
}